package com.fzh.web.yibai.app.controller;

import com.fzh.web.yibai.app.entity.Article;
import com.fzh.web.yibai.app.entity.Banner;
import com.fzh.web.yibai.app.entity.Category;
import com.fzh.web.yibai.app.entity.Website;
import com.fzh.web.yibai.app.service.ArticleService;
import com.fzh.web.yibai.app.service.BannerService;
import com.fzh.web.yibai.app.service.CategoryService;
import com.fzh.web.yibai.app.service.WebsiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import java.util.Collections;
import java.util.List;

@Controller
public class IndexController {


    @Autowired
    private WebsiteService websiteService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private BannerService bannerService;

    /**
     * 首页
     * @return
     */
    @GetMapping("/")
    public ModelAndView index() {
        //网址基本配置信息
        ModelAndView modelAndView = new ModelAndView("index");
        Website website =  websiteService.findSite(1);
        website.setTitles(website.getName()+"-"+website.getSlogan());
        modelAndView.addObject("website",website);

        //首页轮播
        List<Article> articles =  articleService.findArticle(1,5,1);
        modelAndView.addObject("articles",articles);

        //最新推荐4篇文章
        List<Article> articles2 =  articleService.findArticle2(2,0,2,1);
        modelAndView.addObject("articles2",articles2);
        List<Article> articles2_2 =  articleService.findArticle2(2,2,2,1);
        modelAndView.addObject("articles2_2",articles2_2);

        //banner
        Banner banner = bannerService.findBanner(1,1);
        modelAndView.addObject("banner",banner);
        Banner banner2 = bannerService.findBanner(1,2);
        modelAndView.addObject("banner2",banner2);

        //热门文章
        List<Article> articles3 =  articleService.findArticle2(0,0,5,1);
        if(!CollectionUtils.isEmpty(articles3)){
            modelAndView.addObject("homeArticles",articles3.get(0));
            articles3.remove(0);
        }
        modelAndView.addObject("homeArticles2",articles3);

        //资讯分类
        List<Category>  categories = categoryService.findCategory(1);

        if(!CollectionUtils.isEmpty(categories)){
            modelAndView.addObject("categorieName",categories.get(0));
        }

        modelAndView.addObject("categories",categories);
        //友情链接




        //热门文章列表
        List<Article> articles4 =  articleService.findArticle2(0,0,10,1);
        modelAndView.addObject("homeArticlesList",articles4);

        return modelAndView;

    }



    /**
     * 资讯详情
     * @return
     */
    @GetMapping("/article/{id}.html")
    public ModelAndView article(@PathVariable(name = "id") String id) {

        List<Category>  categories = categoryService.findCategory(1);
        ModelAndView modelAndView = new ModelAndView("article/info");
        modelAndView.addObject("categories",categories);
        Website website =  websiteService.findSite(1);
        website.setTitles(website.getName()+"-"+website.getSlogan());
        modelAndView.addObject("website",website);
        Article article = articleService.findArticleById(id);
        modelAndView.addObject("article",article);



        List<Article> articles3 =  articleService.findArticle2(0,0,6,1);
        modelAndView.addObject("articles3",articles3);
        return modelAndView;
    }



    /**
     * 资讯列表
     * @return
     */
    @GetMapping("/articles/{category}/{pageNo}.html")
    public String articleList(@PathVariable(name = "category") String category,
                              @PathVariable(name = "pageNo") Integer pageNo){
        System.out.println("category="+category);
        System.out.println("pageNo="+pageNo);
        return "article/list";
    }
}
